unit DisablePanel;

interface

uses
  Windows, Classes, SysUtils, ExtCtrls, Graphics, Controls, Forms;

Type
  TDisablePanel = Class(TComponent)
    private
      FListControl:TList;
      FPanel: TPanel;
      FImage: TImage;
      FBitMap: TBitmap;
      procedure ConvertToGrayBitmap(bmp: TBitmap);
      procedure Do_DestroyObject;
    public
      procedure Show(aForm:TForm); overload;
      procedure Show(aForm:TForm; ShowControls:array of TControl); overload;
      procedure Close(aForm:TForm);
      Constructor Create(AOwner:TComponent); override;
      Destructor  Destroy; override;
  End;


implementation

{ TDisablePanel }

procedure TDisablePanel.Close(aForm:TForm);
var
  I: Integer;
begin
  for I := 0 to FListControl.Count - 1 do
  begin
    TControl( FListControl.Items[i] ).Parent := aForm;
    TControl( FListControl.Items[i] ).Visible := False;
  end;

  FListControl.Clear;
  Do_DestroyObject;
end;

constructor TDisablePanel.Create(AOwner: TComponent);
begin
  inherited;
  FListControl := TList.Create;
end;

destructor TDisablePanel.Destroy;
begin
  FreeAndNil( FListControl );
  inherited;
end;

procedure TDisablePanel.Do_DestroyObject;
begin
  if Assigned(FImage) then FreeAndNil( FImage );
  if Assigned(FPanel) then FreeAndNil( FPanel );
  if Assigned(FBitMap) then FreeAndNil( FBitMap );
end;

procedure TDisablePanel.ConvertToGrayBitmap(bmp: TBitmap);
var
  pt : PByte;
  gra : Byte;
  h,w : Integer;
begin
  bmp.PixelFormat:=pf32bit;
  gra:=180;
  for h:=0 to bmp.Height-1 do
  begin
    pt:=PByte(bmp.ScanLine[h]);
    for w:=0 to bmp.Width-1 do
    begin
      pt^ := (pt^+gra) div 2;Inc(pt);
      pt^ := (pt^+gra) div 2;Inc(pt);
      pt^ := (pt^+gra) div 2;Inc(pt);
      pt^ := (pt^+gra) div 2;Inc(pt);
    end;
  end;
end;

procedure TDisablePanel.Show(aForm: TForm; ShowControls: array of TControl);
var
  I: Integer;
begin
  self.Show(aForm);
  for I := low(ShowControls) to high(ShowControls) do
  begin
    FListControl.add( ShowControls[i] );
    ShowControls[i].Parent := FPanel;
    ShowControls[i].Visible := True;
    ShowControls[i].BringToFront;
  end;

end;

procedure TDisablePanel.Show(aForm:TForm);
begin
  FPanel := TPanel.Create(Self);
  FPanel.SetBounds(0,0,aForm.ClientWidth, aForm.ClientHeight);
  FPanel.BorderStyle := bsNone;
  FPanel.BevelOuter  := bvNone;

  FBitMap := aForm.GetFormImage();
  FBitMap.PixelFormat := pf32bit;
  ConvertToGrayBitmap(FBitMap);

  FImage := TImage.Create(FPanel);
  FImage.Align := alClient;
  FImage.Picture.Bitmap.Width  := aForm.ClientWidth;
  FImage.Picture.Bitmap.Height := aForm.ClientHeight;
  FImage.Picture.Bitmap.Assign(FBitMap);
  
  FreeAndNil(FBitMap);
  
  FPanel.Parent := aForm;
  FImage.Parent := FPanel;
end;

end.
